package filters

import (
	"gitchina/kly_service/models"
	_ "gitchina/kly_service/util"
	"net/http"

	_ "github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

var auth_map = map[string]byte{
	"101002": 1,
	"101003": 1,
	"101007": 1,
	"101008": 1,
	"101009": 1,
	"101010": 1,
	"101011": 1,
	"101012": 1,
	"101014": 1,
	"101015": 1,
	"101016": 1,
	"101017": 1,
	"101019": 1,
	"101020": 1,
	"101021": 1,
	"102003": 1,
	"102004": 1,
	"102005": 1,
	"102006": 1,
	"102007": 1,
	"102012": 1,
	"102013": 1,
	"102015": 1,
	"102016": 1,
	"102017": 1,
	"102018": 1,
	"102019": 1,
	"102020": 1,
	"102021": 1,
	"102022": 1,
	"102023": 1,
	"102026": 1,
	"102029": 1,
	"102030": 1,
	"102031": 1,
	"102032": 1,
	"102033": 1,
	"102034": 1,
	"103001": 1,
	"103002": 1,
	"103003": 1,
	"103005": 1,
	"103006": 1,
	"103007": 1,
	"103008": 1,
	"103009": 1,
	"103010": 1,
	"103011": 1,
	"103012": 1,
	"103013": 1,
	"103014": 1,
	"103015": 1,
	"103016": 1,
	"103017": 1,
	"103018": 1,
	"103019": 1,
	"103021": 1,
	"103022": 1,
	"104001": 1,
	"104002": 1,
	"104003": 1,
	"104004": 1,
	"104005": 1,
	"104006": 1,
	"104007": 1,
	"104008": 1,
	"104009": 1,
	"105001": 1,
	"105003": 1,
	"105004": 1,
	"105005": 1,
	"105007": 1,
	"105009": 1,
	"105099": 1,
	"105100": 1,
	"105101": 1,
	"108001": 1,
	"108002": 1,
	"108003": 1,
	"108004": 1,
	"108005": 1,
	"108006": 1,
}

var AuthFilterFunc = func(ctx *context.Context) {
	id, ok := ctx.Input.Params()[":id"]
	if ok {
		if _, ok := auth_map[id]; ok {
			var auth models.Auth
			token := ctx.Request.Header.Get("Auth-Token")
			if pass, mbrId, phone, nickName := auth.Authenticate(token); !pass {
				http.Error(ctx.ResponseWriter, "", http.StatusUnauthorized)
			} else {
				ctx.Input.SetData("mbrId", mbrId)
				ctx.Input.SetData("phone", phone)
				ctx.Input.SetData("nickName", nickName)
			}
		}
	}
}
